<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Page;
use app\admin\model\Pagetype;
use think\Request;
use think\File;
class  Pages extends Base
{
    /*
     * 新闻列表
     */
    public function lists(Request $request){
        $user = $this->user_info();
        //查询所有的资讯分类，
        $resource = collection(Pagetype::order("sort asc")->select())->toArray();
        //给查询每个分类下的所有资讯
        foreach($resource as $k => $v){
            $newslist = Page::where(array("Pagetype"=>$v['id']))->order("sorttype asc,id desc")->paginate(10,false,[
                'path'=> "/admin/Pages/lists/typeid/".$v['id'],
                'var_page' => 'page'.$v['id'],
            ]);;
            if(!empty($newslist)) {
                foreach ($newslist as $ks => $vs) {
                    //将逗号全部转化为英文的
                    $vs['key'] = preg_replace("/,|，/",",",$vs['key']);
                    //将关键词拆检为数组
                    $key = explode(",", $vs['key']);
                    //修改原数组结构
                    $newslist[$ks]['key'] = $key;
                }
            }
            $resource[$k]['newslist'] = $newslist;
        }
        $typeid = !empty($request->route("typeid")) ?$request->route("typeid") : $resource[0]['id'];
        $data = array(
            "setting" => $this->getAllSetting(),
            "activeid"=>$typeid,
            "list"=>$resource,
        );
        return view("lists",$data);

    }
    //上传文件
    public function upload(Request $request){
        //接收上传的文件
        $file = $this->request->file('file');
        if(!empty($file)){
            //图片存的路径
            $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'pages';

            // 移动到框架应用根目录/public/uploads/ 目录下

            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
            $error = $file->getError();
            //验证文件后缀后大小
            if(!empty($error)){
                dump($error);exit;
            }
            if($info){
                // 成功上传后 获取上传信息

                //获取图片的名字
                $imgName = $info->getFilename();
                //获取图片的路径
                $photo= $imgName;

            }else{
                // 上传失败获取错误信息
                $file->getError();
            }
        }else{
            $photo = '';
        }
        if($photo !== ''){
            return ['code'=>1,'msg'=>'成功','photo'=>$photo];
        }else{
            return ['code'=>404,'msg'=>'失败'];
        }
    }
    //添加
    public function addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => Pagetype::order("sort asc")->select(),
        );
        return view("addform",$data);
    }
    public function add(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['create_ip'] = $_SERVER['REMOTE_ADDR'];
        $data['author'] = $user['username'];
        $data['photo'] = $user['photo'];
        $res = Page::insert($data);
        if($res){
            $this->success("添加成功","Pages/lists");
        }else{
            $this->error("添加失败","Pages/addform");
        }
    }
    //修改
    public function update(Request $request){
        $user = $this->user_info();
        $id = $request->get("id");
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => Pagetype::select(),
            "data" => Page::where(array("id"=>$id))->find(),
        );

        return view("update",$data);
    }
    public function toUpdate(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $data['update_time'] = time();
        if(Page::update($data)){
            $this->success("修改成功","Pages/lists?typeid=".$request->post('Pagetype'));
        }else{
            $this->error("修改失败");
        }

    }
    //删除
    public function deletes(Request $request){
        $user = $this->user_info();
        if(Page::destroy($request->post('id'))){
            $this->success("删除成功","Pages/lists?typeid=".$request->post('typeid'));
        }else{
            $this->error("删除失败");
        }
    }
    //------------------------------------------------------------------------------------------------------------------
    //单页分类列表
    public function type_lists(){
        $user = $this->user_info();
        //查询所有单页面，不现实状态不考虑因为后期要修改。
        $resource = collection(Pagetype::order("sort asc")->select())->toArray();
        foreach($resource as $k => $v){
            $sum = Page::where(array("pagetype"=>$v['id']))->count();
            $resource[$k]['sum'] = $sum;
        }
        $data = array(
            "setting" => $this->getAllSetting(),
            "list"=>$resource,
        );
        return view("typelist",$data);
    }
    //添加
    public function type_addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
        );
        return view("type_addform",$data);
    }
    public function type_add(Request $request){
        //接收所有的请求数据，数组：多个数据的集合
        $data = $request->post();
        // 创建数组元素
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = Pagetype::insert($data);  //执行数据库命令
        //返回结果的判断
        if($res){
            $this->success("添加成功","Pages/type_lists");
        }else{
            $this->error("添加失败","Pages/type_addform");
        }
    }

    //删除分类
    public function type_deletes(Request $request){
        $user = $this->user_info();
        $id = $request->post('id');
        //删除分类的时先去新闻标中查看该分类下是否有新闻，如果有则不允许删除
        $sum =  Page::where(array("pagetype"=>$id))->count();
        if($sum > 0){
            $this->error("该分类有资讯，请将资讯迁移之后再删除或删除该分类下的所有资讯。");
            exit();
        }
        //如果该分类下没有新闻则直接删除
        if(Pagetype::destroy($id)){
            $this->success("删除成功","Pages/type_lists");
        }else{
            $this->error("删除失败");
        }
    }
    //修改分类状态
    public function setTypeStatus(Request $request){
        $user = $this->user_info();
        if(Pagetype::update($request->post())){
            $this->success("成功","Pages/type_lists");
        }else{
            $this->error("失败");
        }
    }
    //修改分类
    public function type_update(Request $request){
        $user = $this->user_info();
        $id = $request->get("id");
        $data = array(
            "setting" => $this->getAllSetting(),
            "data" => Pagetype::where(array("id"=>$id))->find(),
        );
        return view("type_update",$data);
    }
    public function type_toUpdate(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $data['update_time'] = time();
        if(Pagetype::update($data)){
            $this->success("修改成功","Pages/type_lists");
        }else{
            $this->error("修改失败");
        }

    }

}
